package put.poznan.qmese.matlab.model;

public class Multiplication extends Operation {

	@Override
	public Matrix calculate() throws CalculationException {
		Matrix mA = Memory.getMatrix(getMatrixA());
		Matrix mB = Memory.getMatrix(getMatrixB());
		
		int n1 = mA.getN();
		int m1 = mA.getM();
		int n2 = mB.getN();
		int m2 = mB.getM(); 
		
		if (n1!=m2){
			throw new CalculationException("Different dimensions");
		}
		
		Matrix result = new Matrix();
		result.initialize(n1, m2);
		
		double temp = 0;
		for (int i=0; i<n1; i++){
			for (int j=0; j<m2; j++){
				for (int k=0; k<n2; k++){
					temp +=  mA.get(i, k)*mB.get(k,j);
				}
				result.set(i, j, temp);
				temp = 0;
			}
		}

		return result;
	}
}
